<template>
  <a-modal
    :title="title"
    :width="1250"
    :height="700"
    :visible="visible"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 700;"
  >
    <!-- table区域-begin -->
    <a-table
      bordered
      ref="table"
      size="middle"
      rowKey="itemid"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: getType }"
      :customRow="rowAction"
      @change="handleTableChange"
    >
    </a-table>
  </a-modal>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction } from '@/api/manage'
import Vue from 'vue'
import { stubString } from 'lodash'
export default {
  name: 'AvaiableReportitem',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      title: '操作',
      visible: false,
      disableMixinCreated: true,
      selectedRowKeys: [],
      selectedDetailRowKeys: [],
      selectBillRows: [],
      selectBillDetailRows: [],
      showType: 'basic',
      selectType: 'list',
      linkNumber: '',
      organId: '',
      discount: '',
      deposit: '',
      remark: '',
      vendorid: '',
      warehosueid: '',
      companyid: '',
      queryParam: {
        billtypecode: '',
        materialParam: '',
        type: '',
        subType: '',
        status: ''
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      // 表头
      columns: [
        {
          title: '单据类型',
          dataIndex: 'billtypecode',
          width: 100
        },
        {
          title: '单据类型名称',
          dataIndex: 'billtypename',
          width: 100
        },
        { title: '单据编号', dataIndex: 'billcode', width: 100 },
        {
          title: '可用数量',
          dataIndex: 'leftqty',
          width: 200
        },
        {
          title: '行总数量',
          dataIndex: 'qty',
          width: 200
        },
        {
          title: '不可用数量',
          dataIndex: 'subqty',
          width: 200
        },
        {
          title: '收付方向',
          dataIndex: 'direction',
          width: 100,
          customRender: (text, record, index) => {
            if (text === '0') {
              return '预计收'
            } else if (text === '1') {
              return '预计发'
            } else {
              return '未知'
            }
          }
        }
      ],
      dataSource: [],
      dataSourceDetail: [],
      url: {
        list: '/base/basequeryreport/querybasereportitemarray'
      }
    }
  },
  computed: {
    getType: function() {
      return 'checkbox'
    }
  },
  created() {},
  methods: {
    show(mainid) {
      console.log("mainid:"+mainid);
      this.selectType = 'list'
      this.showType = 'basic'
      this.queryParam.roleType = Vue.ls.get('roleType')
      //this.columns[0].title = organType
      this.model = Object.assign({}, {})
      this.visible = true
      this.queryParam.mainid = mainid
      this.loadData(1)
    },
    myHandleDetail(record) {
      console.log('yyc:' + record)
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    onSelectDetailChange(selectedRowKeys) {
      this.selectedDetailRowKeys = selectedRowKeys
    },
    handleOk() {
      this.getSelectBillRows()
      if (this.selectedRowKeys) {
        this.$emit('ok', this.selectedRowKeys)
      }

      this.close()
    },
    //查询明细列表
    loadDetailData(arg) {
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      if (this.selectBillRows && this.selectBillRows.length > 0) {
        let record = this.selectBillRows[0]
        let param = {
          headerId: record.id,
          mpList: '',
          linkType: this.showType
        }
        this.loading = true
        getAction('/depotItem/getDetailList', param).then(res => {
          if (res.code === 200) {
            let list = res.data.rows
            let listEx = []
            for (let j = 0; j < list.length; j++) {
              let info = list[j]
              //去掉已经全部转换的明细
              if (info.preNumber !== info.finishNumber) {
                listEx.push(info)
              }
            }
            this.dataSourceDetail = listEx
            this.ipagination.total = res.data.total
          }
          if (res.code === 510) {
            this.$message.warning(res.data)
          }
          this.loading = false
        })
      }
    },
    onDateChange: function(value, dateString) {
      this.queryParam.beginTime = dateString[0].substring(0, 10)
      this.queryParam.endTime = dateString[1].substring(0, 10)
    },
    onDateOk(value) {
      console.log(value)
    },
    searchReset() {
      this.queryParam = {
        type: this.queryParam.type,
        subType: this.queryParam.subType,
        status: '1,3',
        vendorid: this.queryParam.vendorid
      }
      this.loadData(1)
    },
    getSelectBillRows() {
      let dataSource = this.dataSource
      this.selectBillRows = []
      for (let i = 0, len = dataSource.length; i < len; i++) {
        if (this.selectedRowKeys.includes(dataSource[i].id)) {
          this.selectBillRows.push(dataSource[i])
        }
      }
    },
    getSelectBillDetailRows() {
      let dataSource = this.dataSourceDetail
      this.selectBillDetailRows = []
      for (let i = 0, len = dataSource.length; i < len; i++) {
        if (this.selectedDetailRowKeys.includes(dataSource[i].id)) {
          this.selectBillDetailRows.push(dataSource[i])
        }
      }
    },
    rowAction(record, index) {
      return {
        on: {
          click: () => {
            let arr = []
            arr.push(record.itemid)
            this.selectedRowKeys = arr
          },
          dblclick: () => {
            let arr = []
            arr.push(record.id)
            this.selectedRowKeys = arr
            this.handleOk()
          }
        }
      }
    }
  }
}
</script>

<style scoped></style>
